﻿using System;
using Nextensions.Text;
using System.Text.RegularExpressions;

namespace Nextensions.Net.HTML
{
    public class Input: Element
    {
        private string _name = String.Empty;
        private string _value = String.Empty;
        private bool _selected = false;
        private InputTypes _inputType;

        public Input(InputTypes type, string name, string value)
        {
            Name = _name = name;
            _inputType = type;
            _value = value;
        }

        public Input(string HTML):base(HTML)
        {
            if (Regex.IsMatch(HTML, @"^\s*<select\b", RegexOptions.IgnoreCase))
                _inputType = InputTypes.Select;
            else if (Regex.IsMatch(HTML, @"^\s*<textarea\s", RegexOptions.IgnoreCase))
                _inputType = InputTypes.TextArea;
            else
                _inputType = (InputTypes)Enum.Parse(typeof(InputTypes), MLUtil.GetAttributeValue(HTML, "Type"), true);

            //Console.WriteLine(_inputType);

            switch (Type)
            { 
                case InputTypes.TextArea:

                    _value = GetTextAreaValue();
                    break;

                case InputTypes.Select:

                    _value = GetSelectValue();
                    break;

                default:

                    _value = MLUtil.GetAttributeValue(HTML, "Value");
                    break;
            }

            if (Type == InputTypes.Checkbox || Type == InputTypes.Radio)
            {
                _selected = Regex.IsMatch(HTML, @"\sCHECKED(\s|>)", RegexOptions.IgnoreCase);
            }
        }
        
        private string GetTextAreaValue()
        {
            return Regex.Match(HTML, @"(?<=>).+(?=</textarea>)", RegexOptions.IgnoreCase).Value;
        }

        private string GetSelectValue()
        {
            string val;
            Match selectedOption = Regex.Match(HTML, @"<option(?<tag>[^>]*SELECTED[^>]*)>(?<text>[^<]*)", RegexOptions.IgnoreCase | RegexOptions.RightToLeft); // In cases where more than one option contains the SELECTED attribute, the last one is the important one.

            if (selectedOption.Success)
            {
                val = MLUtil.GetAttributeValue(selectedOption.Value, "value");

                if (val == null)
                {
                    if (selectedOption.Groups["text"].Success)
                        val = selectedOption.Groups["text"].Value;
                }
            }
            else 
            {
                val = String.Empty;
            }

            return val;
        }

        public string Value
        {
            get
            {
                return _value ?? "";
            }
            set
            {
                _value = value;
            }
        }

        public InputTypes Type
        {
            get
            {
                return _inputType;
            }
        }

        public bool Selected
        {
            get
            {
                return _selected;
            }
        }

        public override string ToString()
        {
            return String.Format("({0}) {1}={2}", Type, Name, Value);
        }
    }
}
